package com.hnevc.netfoodchoose;

        import android.content.Intent;
        import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;

        import org.json.JSONException;
        import org.json.JSONObject;

        import java.io

        .IOException;

        import okhttp3.MediaType;
        import okhttp3.OkHttpClient;
        import okhttp3.Request;
        import okhttp3.RequestBody;
        import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    EditText mEtUserName;
    EditText mEtUserPassword;
    Button mBtnLogin;
    Button mBtnShowFoods;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化控件
        initView();
        initEvent();

    }

    private void initEvent() {
        //TODO 设置按钮的点击事件，完成登陆的验证
        mBtnLogin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //网络访问
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //ToDo网络访问
                        //构造json对象
                        JSONObject object =new JSONObject();

                        try {
                            object.put("userName",mEtUserName.getText().toString());
                            object.put("userPassword",mEtUserPassword.getText().toString());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String s=object.toString();
                        Log.e("TAG","run:"+s);
                        //2.定义请求数据体
                        RequestBody body =RequestBody.create(MediaType.parse("application/json"),s);
                        //3. 创建请求对象
                        Request request =new Request.Builder()
                                .url("http://10.1.66.232:8080/RestaurantServer/user/doLogin ")
                                .post(body).build();
                                //4. 创建客户端，发送请求
                                //OkHttpClient client =new OkHttpClient();
                                OkHttpClient client =new OkHttpClient.Builder()
                                        .cookieJar(new MyCookjejar())
                                        .build();
                        try {
                            Response response =client.newCall(request).execute();
                            // Log.e("TAG","run:"+response.body().string());
                            String data =response.body().string();
                            if (data!=null && data.length()>0){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Intent intent =new Intent();
                                        intent.setClass(MainActivity.this,ShowFoodActivity.class);
                                        startActivity(intent);
                                    }
                                });

                            }else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, "用户名密码错误", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                }).start();
            }
        });
        //TODO 设置显示食品列表按钮事件，跳转到SHowFoodActivity完成食品列表的显示

    }

    private void initView() {
        mBtnShowFoods = (Button) findViewById(R.id.id_btn_show_foods);
        mBtnLogin = (Button) findViewById(R.id.id_btn_login);
        mEtUserName = (EditText) findViewById(R.id.id_tv_user_name);
        mEtUserPassword = (EditText) findViewById(R.id.id_et_user_password);
    }
}
